import fetchData from '../fetch-data/index'

export default function (pageOptions) {
    if (pageOptions) {
        if (!pageOptions.data.hasOwnProperty('searchParams')) { // 搜索条件
            pageOptions.data.searchParams = {};
        }
        //onload重写
        let rOnLoad = pageOptions.onLoad;
        pageOptions.onLoad = function (...args) {
            //必须function
            if (rOnLoad && args && args.length) {
                this.setData({// 当前页面的参数
                    currentPageOptions: { ...args[0] }
                })
            }
            rOnLoad && rOnLoad.call(this, ...args);
        }

        let rOnShow = pageOptions.onShow;
        pageOptions.onShow = function (...args) {
            //必须function
            rOnShow && rOnShow.call(this, ...args);
        };
        let rOnShareAppMessage = pageOptions.onShareAppMessage;
        if (rOnShareAppMessage) {
            pageOptions.onShareAppMessage = function (...args) {
                //必须function
                return rOnShareAppMessage.call(this, ...args);
            };
        }

        /** 设计点击跳转事件*/
        pageOptions.handlerJumpPage = function (ev) {
            wx.$Fun.toPage({
                dataset: ev
            })
        };

        // 拉取数据
        pageOptions.handleFetchData = async function (obj = {}, key = 'apiField', hasLoading = false) {
            obj['pageOptions'] = pageOptions;
            obj['that'] = this;
            await fetchData.handleFetchData(obj, key, hasLoading);
        }

        // 获取列表数据
        pageOptions.handleFetchListData = async function (obj = {}, key = 'listField', hasLoading = false) {
            obj['pageOptions'] = pageOptions;
            obj['that'] = this;
            await fetchData.handleFetchListData(obj, key, hasLoading);
        }
    }

    return pageOptions
}